package com.Jshop.search.listener;

import com.Jshop.search.Service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class GoodListener {

    @Autowired
    private SearchService searchService;

    //处理insert和update的消息
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(
                    value = "leyou.create.index.queue",durable = "true"
            ),
            exchange = @Exchange(
                    value = "leyou.item.exchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC
            ),
            key = {"item.insert","item.update"}
    ))
    public void listenCreate(Long id) throws IOException {
        if (id == null){
            return;
        }

        //创建或更新索引
        this.searchService.crateIndex(id);
    }

    //处理delete消息
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(
                    value = "leyou.delete.index.queue",durable = "true"
            ),
            exchange = @Exchange(
                    value = "leyou.item.exchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC
            ),
            key = {"item.delete"}
    ))
    public void listenDelete(Long id) throws IOException {
        if (id == null){
            return;
        }

        //删除
        this.searchService.delete(id);
    }
}